import { Injectable } from '@angular/core';
import { LvConfig } from '../config';
import { I18N_RESOURCE } from './i18n.resource';

@Injectable({
  providedIn: 'root',
})
export class I18NService {
  /**
   * 获取国际化信息
   * @param key     i18n资源的key
   * @param params  i18n中占位符内要填充的内容
   * @param colon   是否要带冒号输出，默认不带冒号（false）非object类型有效
   */
  // eslint-disable-next-line
  get(key, params = [], colon?): any {
    const { language } = LvConfig;
    const resource = I18N_RESOURCE[language] || {};

    let i18nStr = resource[key] || key;

    params.forEach((param, index): void => {
      i18nStr = i18nStr.replace(`{${index}}`, param);
    });

    if (typeof i18nStr !== 'object') {
      if (colon) {
        if (language === 'zh-cn') {
          i18nStr += '：';
        } else {
          i18nStr += ': ';
        }
      } else {
        i18nStr = String(i18nStr);
      }
    }

    return i18nStr;
  }
}
